/*
  Created on Jul 6, 2005
  (C) 2005 
  @author Michael Long, Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

import snbenchlib.Util;
import snobject.snObject;

public class snInteger extends snObject {

	/**
	 * instantiate a new snInteger with given content
	 * @param _content
	 */
	public snInteger(int i){
		super();
		
		content = new Integer(i);
	}
	
	public snInteger(Integer i){
		content = i;
	}
	
	public String getXmlMimeType() { return MimeTypes.INTEGER; }
	public String getWebMimeType() { return "text/plain"; }
	public String getBinaryMimeType() { return "application/binary"; }
	
	public Integer getInteger(){
		return (Integer)this.content;
	}
	
	public int getInt(){
		return getInteger().intValue();
	}

	public void xmlWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		out.write(((Integer)this.content).toString().getBytes());
		xmlWritePost(out,0);
	}

	public void webWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		step.Node.Indent(out, depth);
		out.write(((Integer)content).toString().getBytes());
	}

	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
		int w = ((Integer)content).intValue();
		/* network byte order */
		for (int i=0 ; i<4 ; i++) {
			out.write((w & 0xff000000)/0x1000000);
			w <<= 8;
		}
	}

	public static snInteger xmlParse(org.w3c.dom.Element e) throws step.parse.SyntaxError {
		if (!e.getTagName().equals("snobject"))
			return null;
		String z = "";
		for (org.w3c.dom.Node n = e.getFirstChild() ; 
		     n != null ; 
		     n = n.getNextSibling()) {
			if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
				org.w3c.dom.Text t = (org.w3c.dom.Text) n;
				z += t.getData();
			}
		}
		z = z.trim();
		try {
			return new snInteger(new Integer(z));
		} catch (NumberFormatException ex) {
			throw new step.parse.SyntaxError("Non-integer string in snbench/integer: [" + z + "]");
		}
	}
}
